<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python高级 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>实现一个简单的框架 | python高级</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../day18/03.模板.html" />
    
    
    <link rel="prev" href="../day18/01.框架是什么.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="8.2"
        data-chapter-title="实现一个简单的框架"
        data-filepath="day18/02.实现框架.md"
        data-basepath=".."
        data-revision="Wed Sep 26 2018 22:46:13 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="day11/00.readme.html">
            
                
                    <a href="../day11/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Python语法高级专题1
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="day11/01.GIL.html">
            
                
                    <a href="../day11/01.GIL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        GIL问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="day11/02.copy.html">
            
                
                    <a href="../day11/02.copy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        赋值-浅拷贝-深拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="day11/03.import.html">
            
                
                    <a href="../day11/03.import.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        import导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="day11/04.review.html">
            
                
                    <a href="../day11/04.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="day12/00.readme.html">
            
                
                    <a href="../day12/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        Python语法高级专题2
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="day12/01.mro.html">
            
                
                    <a href="../day12/01.mro.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        多继承和MRO顺序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="day12/02.property.html">
            
                
                    <a href="../day12/02.property.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        property属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="day12/03.magic.html">
            
                
                    <a href="../day12/03.magic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        魔法属性和方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="day12/04.with.html">
            
                
                    <a href="../day12/04.with.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        with管理上下文
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="day12/05.review.html">
            
                
                    <a href="../day12/05.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="day13/00.readme.html">
            
                
                    <a href="../day13/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Python语法之装饰器
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="day13/01.closure.html">
            
                
                    <a href="../day13/01.closure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="day13/02.decorator.html">
            
                
                    <a href="../day13/02.decorator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="day13/03.review.html">
            
                
                    <a href="../day13/03.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="day14/00.readme.html">
            
                
                    <a href="../day14/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        MySQL数据库基本操作
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="day14/01.数据库概述.html">
            
                
                    <a href="../day14/01.数据库概述.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        数据库概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="day14/02.数据类型和约束.html">
            
                
                    <a href="../day14/02.数据类型和约束.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        数据完整性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="day14/03.Navicat.html">
            
                
                    <a href="../day14/03.Navicat.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        图形化客户端Navicat
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="day14/04.命令行客户端.html">
            
                
                    <a href="../day14/04.命令行客户端.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        命令行客户端SQL的使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.1" data-path="day14/05.sql_con.html">
            
                
                    <a href="../day14/05.sql_con.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.1.</b>
                        
                        登录和退出数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.2" data-path="day14/07.sql_table_struct.html">
            
                
                    <a href="../day14/07.sql_table_struct.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.2.</b>
                        
                        表结构的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.3" data-path="day14/06.sql_database.html">
            
                
                    <a href="../day14/06.sql_database.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.3.</b>
                        
                        数据库的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.4" data-path="day14/08.表数据操作.html">
            
                
                    <a href="../day14/08.表数据操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.4.</b>
                        
                        表数据的操作
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="day14/09.review.html">
            
                
                    <a href="../day14/09.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        知识总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="day15/00.readme.html">
            
                
                    <a href="../day15/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        MYSQL数据库进阶操作
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="day15/01.练习.html">
            
                
                    <a href="../day15/01.练习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        练习强化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="day15/02.where条件.html">
            
                
                    <a href="../day15/02.where条件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        条件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="day15/03.排序查询.html">
            
                
                    <a href="../day15/03.排序查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="day15/04.分页查询.html">
            
                
                    <a href="../day15/04.分页查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="day15/05.聚合函数.html">
            
                
                    <a href="../day15/05.聚合函数.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        聚合函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="day15/06.分组.html">
            
                
                    <a href="../day15/06.分组.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        分组
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="day15/07.连接.html">
            
                
                    <a href="../day15/07.连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="day15/08.自连接.html">
            
                
                    <a href="../day15/08.自连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="day15/09.子查询.html">
            
                
                    <a href="../day15/09.子查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        子查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.10" data-path="day15/10.总结.html">
            
                
                    <a href="../day15/10.总结.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.10.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="day16/readme.html">
            
                
                    <a href="../day16/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        MYSQL数据库编程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="day16/data.html">
            
                
                    <a href="../day16/data.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        数据准备
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="day16/sql_code.html">
            
                
                    <a href="../day16/sql_code.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        SQL演练
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="day16/dbdesign.html">
            
                
                    <a href="../day16/dbdesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="day16/pymysql.html">
            
                
                    <a href="../day16/pymysql.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        数据库编程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="day16/zongjie.html">
            
                
                    <a href="../day16/zongjie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="day17/00.readme.html">
            
                
                    <a href="../day17/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        MySQL数据库高级用法
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="day17/01.view.html">
            
                
                    <a href="../day17/01.view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="day17/02.transaction.html">
            
                
                    <a href="../day17/02.transaction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        事务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="day17/03.sql_index.html">
            
                
                    <a href="../day17/03.sql_index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        索引
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="day17/04.user.html">
            
                
                    <a href="../day17/04.user.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        用户管理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="day17/05.master_slave.html">
            
                
                    <a href="../day17/05.master_slave.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        主从配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.6" data-path="day17/06.review.html">
            
                
                    <a href="../day17/06.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.6.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="day18/00.readme.html">
            
                
                    <a href="../day18/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        miniweb框架
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="day18/01.框架是什么.html">
            
                
                    <a href="../day18/01.框架是什么.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        框架概念
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="8.2" data-path="day18/02.实现框架.html">
            
                
                    <a href="../day18/02.实现框架.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        实现一个简单的框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="day18/03.模板.html">
            
                
                    <a href="../day18/03.模板.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        模板替换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="day18/04.路由.html">
            
                
                    <a href="../day18/04.路由.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        路由概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="day18/05.显示股票数据.html">
            
                
                    <a href="../day18/05.显示股票数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        股票收藏夹
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="day18/05.review.html">
            
                
                    <a href="../day18/05.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        复习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python高级</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="2-&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x6846;&#x67B6;">2. &#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x6846;&#x67B6;</h1>
<h2 id="21-&#x4FEE;&#x6539;web&#x670D;&#x52A1;&#x5668;&#x652F;&#x6301;&#x8C03;&#x7528;&#x6846;&#x67B6;">2.1 &#x4FEE;&#x6539;web&#x670D;&#x52A1;&#x5668;&#x652F;&#x6301;&#x8C03;&#x7528;&#x6846;&#x67B6;</h2>
<pre><code class="lang-py"><span class="hljs-keyword">import</span> socket
<span class="hljs-keyword">import</span> re
<span class="hljs-keyword">import</span> gevent
<span class="hljs-keyword">from</span> gevent <span class="hljs-keyword">import</span> monkey
<span class="hljs-keyword">import</span> sys

<span class="hljs-comment"># &#x8BA9;gevent&#x80FD;&#x591F;&#x8BC6;&#x522B;&#x8017;&#x65F6;&#x64CD;&#x4F5C;&#xFF0C;&#x8BA9;&#x534F;&#x7A0B;&#x81EA;&#x52A8;&#x5207;&#x6362;&#x6267;&#x884C;&#xFF0C;&#x8BC6;&#x522B;&#x7684;&#x8017;&#x65F6;&#x64CD;&#x4F5C;&#x7531;&#xFF1A; recv, accept, time.sleep, &#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x5EF6;&#x65F6;</span>
monkey.patch_all()


<span class="hljs-comment"># &#x5C01;&#x88C5;&#x7684;web&#x670D;&#x52A1;&#x5668;&#x7C7B;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HttpWebServer</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, port)</span>:</span>
        <span class="hljs-comment"># &#x521B;&#x5EFA;tcp&#x670D;&#x52A1;&#x7AEF;&#x5957;&#x63A5;&#x5B57;</span>
        tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        <span class="hljs-comment"># &#x8BBE;&#x7F6E;socket&#x9009;&#x9879;&#xFF0C;&#x7ACB;&#x5373;&#x91CA;&#x653E;&#x7AEF;&#x53E3;</span>
        tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, <span class="hljs-keyword">True</span>)
        <span class="hljs-comment"># &#x7ED1;&#x5B9A;&#x7AEF;&#x53E3;&#x53F7;</span>
        tcp_server_socket.bind((<span class="hljs-string">&quot;&quot;</span>, port))
        <span class="hljs-comment"># &#x8BBE;&#x7F6E;&#x76D1;&#x542C;</span>
        tcp_server_socket.listen(<span class="hljs-number">128</span>)
        <span class="hljs-comment"># &#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x63D0;&#x4F9B;socket&#x5C5E;&#x6027;</span>
        self.tcp_server_socket = tcp_server_socket

    <span class="hljs-comment"># &#x542F;&#x52A8;&#x670D;&#x52A1;&#x5668;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-comment"># &#x5FAA;&#x73AF;&#x63A5;&#x6536;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8FDE;&#x63A5;&#x8BF7;&#x6C42;</span>
        <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
            service_client_socket, ip_port = self.tcp_server_socket.accept()
            <span class="hljs-comment"># handle_client_request(service_client_socket)</span>
            <span class="hljs-comment"># &#x5F00;&#x8F9F;&#x534F;&#x7A0B;&#x5E76;&#x6267;&#x884C;&#x5BF9;&#x5E94;&#x7684;&#x4EFB;&#x52A1;</span>
            gevent.spawn(self.handle_client_request, service_client_socket)
            <span class="hljs-comment"># &#x4E0D;&#x9700;&#x8981;&#x52A0;&#x4E0A;join&#xFF0C;&#x4E3B;&#x8981;&#x539F;&#x6709;&#x662F;&#x6211;&#x4EEC;&#x7684;&#x8FDB;&#x7A0B;&#x4E0D;&#x4F1A;&#x9000;&#x51FA;</span>
            <span class="hljs-comment"># g1.join()</span>

    <span class="hljs-comment"># &#x5904;&#x7406;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8BF7;&#x6C42;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">handle_client_request</span><span class="hljs-params">(self, service_client_socket)</span>:</span>
        <span class="hljs-comment"># &#x83B7;&#x53D6;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8BF7;&#x6C42;&#x62A5;&#x6587;&#x6570;&#x636E;</span>
        client_request_data = service_client_socket.recv(<span class="hljs-number">4096</span>)
        print(client_request_data)
        <span class="hljs-comment"># GET /index2.html HTTP/1.1xxxxxxx</span>
        client_request_conent = client_request_data.decode(<span class="hljs-string">&quot;utf-8&quot;</span>)
        <span class="hljs-comment"># &#x901A;&#x8FC7;&#x6B63;&#x5219;&#x67E5;&#x627E;&#x8BF7;&#x6C42;&#x7684;&#x8D44;&#x6E90;&#x8DEF;&#x5F84;</span>
        match_obj = re.search(<span class="hljs-string">&quot;/\S*&quot;</span>, client_request_conent)

        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> match_obj:
            print(<span class="hljs-string">&quot;&#x8BBF;&#x95EE;&#x8DEF;&#x5F84;&#x6709;&#x8BEF;&quot;</span>)
            service_client_socket.close()
            <span class="hljs-keyword">return</span>

        <span class="hljs-comment"># &#x83B7;&#x53D6;&#x5339;&#x914D;&#x7ED3;&#x679C;</span>
        request_path = match_obj.group()
        print(request_path)

        <span class="hljs-keyword">if</span> request_path == <span class="hljs-string">&quot;/&quot;</span>:
            <span class="hljs-comment"># &#x5982;&#x679C;&#x7528;&#x6237;&#x6CA1;&#x6709;&#x6307;&#x5B9A;&#x8D44;&#x6E90;&#x8DEF;&#x5F84;&#x90A3;&#x4E48;&#x9ED8;&#x8BA4;&#x8BBF;&#x95EE;&#x7684;&#x6570;&#x636E;&#x662F;&#x9996;&#x9875;&#x7684;&#x6570;&#x636E;</span>
            request_path = <span class="hljs-string">&quot;/index.html&quot;</span>
        <span class="hljs-keyword">if</span> request_path.endswith(<span class="hljs-string">&quot;.html&quot;</span>):
            <span class="hljs-comment"># &#x5224;&#x65AD;&#x7528;&#x6237;&#x7684;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84; &#x662F;&#x5426;&#x662F;&#x4EE5;.html&#x7ED3;&#x5C3E;&#x7684; &#x662F;--&#x662F;&#x52A8;&#x6001;&#x8D44;&#x6E90;&#x8BF7;&#x6C42;</span>
            env = {
                <span class="hljs-string">&apos;PATH_INFO&apos;</span>: request_path
            }
            <span class="hljs-keyword">import</span> Application
            <span class="hljs-comment"># app&#x8FD4;&#x56DE;&#x503C;&#x5C31;&#x662F;&#x54CD;&#x5E94;&#x72B6;&#x6001;,&#x54CD;&#x5E94;&#x5934;,&#x54CD;&#x5E94;&#x4F53; </span>
            status,headers,response_body = Application.app(env)
            response_data = <span class="hljs-string">&quot;HTTP/1.1 %s\r\n&quot;</span> % status
            <span class="hljs-keyword">for</span> header <span class="hljs-keyword">in</span> headers:
                response_data += <span class="hljs-string">&quot;%s: %s\r\n&quot;</span> % header
            response_data = response_data + <span class="hljs-string">&quot;\r\n&quot;</span> + response_body
            service_client_socket.send(response_data.encode())
            service_client_socket.close()
        <span class="hljs-keyword">else</span>:
            <span class="hljs-comment"># &#x8BFB;&#x53D6;&#x6307;&#x5B9A;&#x6587;&#x4EF6;&#x6570;&#x636E;</span>
            <span class="hljs-comment"># &#x4F7F;&#x7528;rb&#x7684;&#x539F;&#x56E0;&#x662F;&#x6D4F;&#x89C8;&#x5668;&#x4E5F;&#x6709;&#x53EF;&#x80FD;&#x8BF7;&#x6C42;&#x7684;&#x662F;&#x56FE;&#x7247;</span>
            <span class="hljs-keyword">try</span>:
                <span class="hljs-keyword">with</span> open(<span class="hljs-string">&quot;static&quot;</span> + request_path, <span class="hljs-string">&quot;rb&quot;</span>) <span class="hljs-keyword">as</span> file:
                    <span class="hljs-comment"># &#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x6570;&#x636E;</span>
                    file_data = file.read()
            <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
                <span class="hljs-comment"># &#x51C6;&#x5907;&#x54CD;&#x5E94;&#x62A5;&#x6587;&#x6570;&#x636E;</span>
                <span class="hljs-comment"># &#x54CD;&#x5E94;&#x884C;</span>
                response_line = <span class="hljs-string">&quot;HTTP/1.1 404 Not Found\r\n&quot;</span>
                <span class="hljs-comment"># &#x54CD;&#x5E94;&#x5934;</span>
                response_header = <span class="hljs-string">&quot;Server: PWS1.0\r\nContent-Type: text/html;charset=utf-8\r\n&quot;</span>
                <span class="hljs-comment"># &#x54CD;&#x5E94;&#x4F53; -&gt; &#x6253;&#x5F00;&#x4E00;&#x4E2A;404html&#x6570;&#x636E;&#x628A;&#x6570;&#x636E;&#x7ED9;&#x6D4F;&#x89C8;&#x5668;</span>
                response_body = <span class="hljs-string">&quot;&lt;h1&gt;&#x975E;&#x5E38;&#x62B1;&#x6B49;&#xFF0C;&#x60A8;&#x5F53;&#x524D;&#x8BBF;&#x95EE;&#x7684;&#x7F51;&#x9875;&#x5DF2;&#x7ECF;&#x4E0D;&#x5B58;&#x5728;&#x4E86;&lt;/h1&gt;&quot;</span>.encode(<span class="hljs-string">&quot;utf-8&quot;</span>)

                <span class="hljs-comment"># &#x5339;&#x914D;&#x54CD;&#x5E94;&#x62A5;&#x6587;&#x6570;&#x636E;</span>
                response_data = (response_line + response_header + <span class="hljs-string">&quot;\r\n&quot;</span>).encode(<span class="hljs-string">&quot;utf-8&quot;</span>) + response_body
                <span class="hljs-comment"># &#x53D1;&#x9001;&#x54CD;&#x5E94;&#x62A5;&#x6587;&#x6570;&#x636E;</span>
                service_client_socket.send(response_data)
            <span class="hljs-keyword">else</span>:
                <span class="hljs-comment"># &#x51C6;&#x5907;&#x54CD;&#x5E94;&#x62A5;&#x6587;&#x6570;&#x636E;</span>
                <span class="hljs-comment"># &#x54CD;&#x5E94;&#x884C;</span>
                response_line = <span class="hljs-string">&quot;HTTP/1.1 200 OK\r\n&quot;</span>
                <span class="hljs-comment"># &#x54CD;&#x5E94;&#x5934;</span>
                response_header = <span class="hljs-string">&quot;Server: PWS1.0\r\nContent-Type: text/html;charset=utf-8\r\n&quot;</span>
                <span class="hljs-comment"># &#x54CD;&#x5E94;&#x4F53;</span>
                response_body = file_data

                <span class="hljs-comment"># &#x5339;&#x914D;&#x54CD;&#x5E94;&#x62A5;&#x6587;&#x6570;&#x636E;</span>
                response_data = (response_line + response_header + <span class="hljs-string">&quot;\r\n&quot;</span>).encode(<span class="hljs-string">&quot;utf-8&quot;</span>) + response_body
                <span class="hljs-comment"># &#x53D1;&#x9001;&#x54CD;&#x5E94;&#x62A5;&#x6587;&#x6570;&#x636E;</span>
                service_client_socket.send(response_data)
            <span class="hljs-keyword">finally</span>:
                service_client_socket.close()

<span class="hljs-comment"># &#x4E3B;&#x51FD;&#x6570;</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
    print(sys.argv)
    <span class="hljs-keyword">if</span> len(sys.argv) != <span class="hljs-number">2</span>:
        print(<span class="hljs-string">&quot;&#x542F;&#x52A8;&#x547D;&#x4EE4;&#x5982;&#x4E0B;: python3 xxx.py 9090&quot;</span>)
        <span class="hljs-keyword">return</span>
    <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> sys.argv[<span class="hljs-number">1</span>].isdigit():
        print(<span class="hljs-string">&quot;&#x542F;&#x52A8;&#x547D;&#x4EE4;&#x5982;&#x4E0B;: python3 xxx.py 9090&quot;</span>)
        <span class="hljs-keyword">return</span>
    port = int(sys.argv[<span class="hljs-number">1</span>])
    print(port)

    server = HttpWebServer(port)
    server.start()


<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    main()
</code></pre>
<h2 id="22-&#x4E00;&#x4E2A;&#x6846;&#x67B6;&#x7684;&#x793A;&#x4F8B;">2.2 &#x4E00;&#x4E2A;&#x6846;&#x67B6;&#x7684;&#x793A;&#x4F8B;</h2>
<pre><code class="lang-py"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">app</span><span class="hljs-params">(data_dict)</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;200 OK&apos;</span>,[(<span class="hljs-string">&apos;Content-Type&apos;</span>,<span class="hljs-string">&apos;text/html&apos;</span>)],<span class="hljs-string">&apos;hello this is response body&apos;</span>
</code></pre>
<h2 id="23-&#x7ED9;&#x6846;&#x67B6;&#x589E;&#x52A0;&#x4E00;&#x4E2A;&#x54CD;&#x5E94;&#x52A8;&#x6001;&#x8D44;&#x6E90;&#x7684;&#x529F;&#x80FD;">2.3 &#x7ED9;&#x6846;&#x67B6;&#x589E;&#x52A0;&#x4E00;&#x4E2A;&#x54CD;&#x5E94;&#x52A8;&#x6001;&#x8D44;&#x6E90;&#x7684;&#x529F;&#x80FD;</h2>
<pre><code class="lang-py"><span class="hljs-keyword">import</span> time


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">gettime</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;&#x5F53;&#x7528;&#x6237;&#x8BF7;&#x6C42;/gettime.html&#x7684;&#x65F6;&#x5019; &#x6267;&#x884C;&#x5F53;&#x524D;&#x51FD;&#x6570;&apos;&apos;&apos;</span>
    <span class="hljs-keyword">return</span> time.ctime()

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">app</span><span class="hljs-params">(data_dict)</span>:</span>
    <span class="hljs-keyword">if</span> data_dict[<span class="hljs-string">&apos;PATH_INFO&apos;</span>] == <span class="hljs-string">&apos;/gettime.html&apos;</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;200 OK&apos;</span>,[(<span class="hljs-string">&apos;Content-Type&apos;</span>,<span class="hljs-string">&apos;text/html&apos;</span>)],time.time()
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;404 Not Found&apos;</span>,[(<span class="hljs-string">&apos;Content-Type&apos;</span>,<span class="hljs-string">&apos;text/html&apos;</span>)],<span class="hljs-string">&apos;hello this is response body&apos;</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../day18/01.框架是什么.html" class="navigation navigation-prev " aria-label="Previous page: 框架概念"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../day18/03.模板.html" class="navigation navigation-next " aria-label="Next page: 模板替换"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python高级 -->
</html>
